package com.aleaho.cftrack.presenter;

import android.content.Context;
import android.net.NetworkInfo;
import android.support.annotation.MainThread;
import android.util.Log;

import com.aleaho.cftrack.CFTrackApplication;
import com.aleaho.cftrack.R;
import com.aleaho.cftrack.http.ApiResponseWraperNoList;
import com.aleaho.cftrack.http.NetWorkUtil;
import com.aleaho.cftrack.http.RequestParam;
import com.aleaho.cftrack.http.api.SignApi;
import com.aleaho.cftrack.http.entity.SignEntity;
import com.aleaho.cftrack.utils.Constants;
import com.aleaho.cftrack.view.SignView;
import com.baidu.mapapi.NetworkUtil;

import rx.Observer;
import rx.android.MainThreadSubscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author Aleaho
 * @version 1.0
 * @date 2017/11/25 21:57
 * @desc
 */
public class SignPresenter {


    private SignView signView = null;
    private Context context = null;

    public SignPresenter(Context context, SignView signView) {
        this.context = context;
        this.signView = signView;
    }


    public void signInOrOut(final SignEntity signEntity) {

        RequestParam param = new RequestParam();
        param.put("signer", signEntity.getSigner());
        param.put("sign_type", signEntity.getSign_type());
        param.put("year", signEntity.getYear());
        param.put("month", signEntity.getMonth());
        param.put("day", signEntity.getDay());
        param.put("hour", signEntity.getHour());
        param.put("minute", signEntity.getMinute());
        param.put("distance", signEntity.getDistance());
        param.put("address", signEntity.getAddress());
        param.put("lat", signEntity.getLat());
        param.put("lng", signEntity.getLng());

        NetWorkUtil.getSignApi().sign(param)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<ApiResponseWraperNoList<SignEntity>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(Constants.TAG, e.getMessage());
                        signView.showMessage(e.getMessage());
                    }

                    @Override
                    public void onNext(ApiResponseWraperNoList<SignEntity> entity) {

                        if (entity.getCode() == Constants.SERVER_ERROR_CODE) {
                            if (signEntity.getSign_type() == Constants.SIGN_IN) {
                                String msg = context.getResources().getString(R.string.sign_in_fail);
                                signView.showMessage(msg);
                            } else if (signEntity.getSign_type() == Constants.SIGN_OUT) {
                                String msg = context.getResources().getString(R.string.sign_out_fail);
                                signView.showMessage(msg);
                            }
                        } else {
                            if (signEntity.getSign_type() == Constants.SIGN_IN) {
                                String msg = context.getResources().getString(R.string.sign_in_success);
                                signView.showMessage(msg);
                            } else if (signEntity.getSign_type() == Constants.SIGN_OUT) {
                                String msg = context.getResources().getString(R.string.sign_out_success);
                                signView.showMessage(msg);
                            }
                        }
                    }
                });
    }

}
